package com.southwind.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.southwind.common.Cache;
import com.southwind.common.ResultObj;
import com.southwind.entity.Admin;
import com.southwind.entity.User;
import com.southwind.entity.Worker;
import com.southwind.form.AccountForm;
import com.southwind.mapper.CarMapper;
import com.southwind.service.AdminService;
import com.southwind.service.UserService;
import com.southwind.service.WorkerService;
import com.southwind.vo.UserVO;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedirectController {

    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private WorkerService workerService;

    @GetMapping("/getCode")
    public String getCode(){
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        String code = specCaptcha.text().toLowerCase();
        Cache.map.put("code", code);
        return specCaptcha.toBase64();
    }

    @Autowired
    private CarMapper carMapper;

    @GetMapping("/login")
    public ResultObj login(AccountForm accountForm){
        //验证用户名
        switch (accountForm.getType()){
            case 1:
                //查询user
                QueryWrapper<User> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("username", accountForm.getLoginname());
                User user = this.userService.getOne(queryWrapper);
                if(user == null) return ResultObj.LOGIN_ERROR_LOGINNAME;
                //验证密码
                if(!user.getPassword().equals(accountForm.getPassword())) return ResultObj.LOGIN_ERROR_PASSWORD;
                //验证code
                if(!Cache.map.get("code").toString().equals(accountForm.getCode())) return ResultObj.LOGIN_ERROR_CODE;
                Cache.map.put("type", accountForm.getType());
                Cache.map.put("user", user);
                ResultObj loginSuccess = ResultObj.LOGIN_SUCCESS;
                loginSuccess.setType(accountForm.getType());
                UserVO userVO = new UserVO();
                BeanUtils.copyProperties(user, userVO);
                userVO.setCarNumber(this.carMapper.getCarNumberById(user.getCid()));
                loginSuccess.setUser(userVO);
                return loginSuccess;
            case 2:
                QueryWrapper<Worker> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("username", accountForm.getLoginname());
                Worker worker = this.workerService.getOne(queryWrapper2);
                if(worker == null) return ResultObj.LOGIN_ERROR_LOGINNAME;
                //验证密码
                if(!worker.getPassword().equals(accountForm.getPassword())) return ResultObj.LOGIN_ERROR_PASSWORD;
                //验证code
                if(!Cache.map.get("code").toString().equals(accountForm.getCode())) return ResultObj.LOGIN_ERROR_CODE;
                Cache.map.put("type", accountForm.getType());
                Cache.map.put("user", worker);
                ResultObj loginSuccess2 = ResultObj.LOGIN_SUCCESS;
                loginSuccess2.setType(accountForm.getType());
                loginSuccess2.setUser(worker);
                return loginSuccess2;
            case 3:
                QueryWrapper<Admin> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("username", accountForm.getLoginname());
                Admin admin = this.adminService.getOne(queryWrapper1);
                if(admin == null) return ResultObj.LOGIN_ERROR_LOGINNAME;
                //验证密码
                if(!admin.getPassword().equals(accountForm.getPassword())) return ResultObj.LOGIN_ERROR_PASSWORD;
                //验证code
                if(!Cache.map.get("code").toString().equals(accountForm.getCode())) return ResultObj.LOGIN_ERROR_CODE;
                Cache.map.put("type", accountForm.getType());
                Cache.map.put("user", admin);
                ResultObj loginSuccess1 = ResultObj.LOGIN_SUCCESS;
                loginSuccess1.setType(accountForm.getType());
                loginSuccess1.setUser(admin);
                return loginSuccess1;
        }
        return null;
    }

}
